package cn.danger.com.presenter;

import android.content.Context;
import android.content.Intent;

import java.io.File;
import java.util.Map;

import cn.bmob.v3.BmobUser;
import cn.danger.com.app.MyApplication;
import cn.danger.com.db.MemoDaoImpl;
import cn.danger.com.model.action.UserInfoModel;
import cn.danger.com.model.action.UserInfoModelImpl;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Constants;
import cn.danger.com.util.FileCacheService;
import cn.danger.com.view.CommonView;

/**
 * Created by 俊 on 2016/12/28.
 */
public class UserInfoPresenterImpl implements UserInfoPresenter {

    private Context context;
    private CommonView view;
    private UserInfoModel model;

    public UserInfoPresenterImpl(Context context, CommonView view) {
        this.context = context;
        this.view = view;
        model = new UserInfoModelImpl(context);
    }

    @Override
    public void modifyPhoto(File photo) {
        view.showDialog();
        model.uploadPhoto(photo, new OnCommonListener() {
            @Override
            public void onSuccess(String url) {
                updatePhoto(url);
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showMessage(error);
            }
        });
    }

    @Override
    public void updatePhoto(final String url) {
        model.updatePhoto(url, new OnCommonListener() {
            @Override
            public void onSuccess(String message) {
                new FileCacheService(context).updatePhoto(url);
                view.hideDialog();
                view.showMessage(Constants.SUCCESS_TO_UPDATE_PHOTO);
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showMessage(error);
            }
        });
    }

    @Override
    public void logout() {
        BmobUser.logOut();
        Map<String, String> map = (Map<String, String>) new FileCacheService(context).getSharePreference("userInfo");
//        new MemoDaoImpl(context).delete(map.get("phone"));
        new FileCacheService(context).deleteUserInfo();
        MyApplication.setOperationCode(Constants.NOT_LOGIN);
        Intent intent = new Intent("update_ui");
        context.sendBroadcast(intent);
    }
}
